package net.xdclass.service.impl;

import net.xdclass.enums.DomainTypeEnum;
import net.xdclass.interceptor.LoginInterceptor;
import net.xdclass.manager.DomainManager;
import net.xdclass.model.DomainDO;
import net.xdclass.service.DomainService;
import net.xdclass.vo.DomainVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DomainServiceImpl implements DomainService {
	@Autowired
	private DomainManager domainManager;
	@Override
	public List<DomainDO> listAll() {
		Long accountNo = LoginInterceptor.threadLocal.get().getAccountNo();
		List <DomainDO> customerDomainlist = domainManager.listCustomDomain(accountNo);
		List <DomainDO> officialDomainlist = domainManager.listOfficialDomain();
		customerDomainlist.addAll(officialDomainlist);
		customerDomainlist.stream().map(obj->beanProcess(obj)).collect(Collectors.toList());
		return officialDomainlist;
	}

	@Override
	public int add(DomainDO domainDO) {
		Long accountno = LoginInterceptor.threadLocal.get().getAccountNo();
		domainDO.setAccountNo(accountno);
		return domainManager.addDomain(domainDO);
	}

	@Override
	public DomainDO findByDomainTypeAndID(long id, DomainTypeEnum domainTypeEnum) {
		DomainDO domainDO = domainManager.findByDomainTypeAndID(id,domainTypeEnum);
		return domainDO;
	}

	public  DomainVO beanProcess(DomainDO domainDO){
		DomainVO domainVO = new DomainVO();
		BeanUtils.copyProperties(domainDO,domainVO);
        return  domainVO;
	}
}
